package com.letv.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class StringUtil {
	
	public final static String COMMA = ",";
	private final static String defaultTime = "1900-01-01 00:00:00";
	
	public static String ifNull(String str) {
		if (str == null) {
			return "";
		} else {
			return str;
		}
	}
	
	/**
	 * 将一个字符串的首字母改成大写
	 * @param str
	 * @return
	 */
	public static String toFirstLetterUpperCase(String str) {
		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, 1).toUpperCase());
		sb.append(str.substring(1));
		return sb.toString();
	}
	
	/**
	 * 将一个字符串的首字母改成小写
	 * @param str
	 * @return
	 */
	public static String toFirstLetterLowerCase(String str) {
		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, 1).toLowerCase());
		sb.append(str.substring(1));
		return sb.toString();
	}
	
	/**
	 * 将一个字符串的首字母改成大写,其他字母为小写
	 * @param str
	 * @return
	 */
	public static String toFirstLetterUpperCaseAndOtherLowwer(String str) {
		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, 1).toUpperCase());
		sb.append(str.substring(1).toLowerCase());
		return sb.toString();
	}
	
	/**
	 * 将列名转换成属性名
	 * @param columnName
	 * @return
	 */
	public static String convertColumnNameToAttrName(String columnName) {
		StringBuilder sb = new StringBuilder();
		String[] arr = columnName.split("_");
		boolean isFirst = true;
		
		for (String str : arr) {
			if (isFirst) {
				sb.append(str);
				isFirst = false;
			} else {
				sb.append(toFirstLetterUpperCase(str));
			}
		}
		return sb.toString();
	}
	
	/**
	 * 判断给定字符串是否符合属性命名规范
	 * 首字母小写的驼峰命名规范
	 * @param str
	 * @return
	 */
	public static boolean isPropertyName(String str) {
		boolean flag = true ;
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			//判断字符是否为字母
			if(!Character.isLetter(ch)) {
				flag = false;
				break;
			}
			//判断首字母是否小写
			if(i == 0 && Character.isUpperCase(ch)) {
				flag = false;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 把list中的字符串给你加上你想加的隔断,比如逗号.
	 * @param list
	 * @param delim
	 * @return
	 */
	public static String getListToString(List<String> list, String delim) {
		if (null == list || list.isEmpty()) {
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		
		for(String str : list) {
			sb.append(str);
			sb.append(delim);
		}
		
		String returnStr = null;
		String tempStr = sb.toString();
		if (StringUtils.isNotBlank(tempStr)) {
			returnStr = tempStr.substring(tempStr.length() - 1);
		}
		return returnStr;
	}
	
	public static String formatString(Object o) {
		if (o != null && !o.equals("")) {
			return "'" + o.toString().replace("\\", "\\\\").replace("'", "\\'").replace("\"", "\\\"") + "'";
		}
		return "''";
	}
	
	public static String convertObjectToString(Object o) {
		if (o != null && !o.equals("")) {
			return o.toString();
		}
		return "";
	}
	
	/****
	 * ""返回0
	 * @param o
	 * @return
	 * @author GaoJiawei
	 */
	public static int formatStringToInt(String o) {
		int result = 0;
		if (null != o) {
			o = o.trim();
		}
		if (StringUtils.isNumeric(o) && !StringUtils.isBlank(o)) {
			result = Integer.parseInt(o);
		}
		return result;
	}
	
	/***
	 * 新媒资数据库int型字段 defult为NULL
	 * @param o
	 * @return
	 */
	public static int getInt(Object o) {
		int result =0;
		result = null == o ?  0:(Integer)o;
		return result;
	}
	
	public static int convertObjectToInteger(Object o) {
		if (o != null && !o.equals("")) {
			try {
				return Integer.parseInt(o.toString());
			} catch (Exception e) {
				return 0;
			}
		}
		return 0;
	}
	
	public static String formatTime(Object o) {
		if (o != null && !o.equals("")) {
			return "'" + o.toString() + "'";
		}
		return "'" + defaultTime + "'";
	}

	/**
	 * 将传入对象转为标准日期格式:yyyy-MM-dd hh:mm:ss
	 * @param o Object, 待转换对象
	 * @param ifAllowNull boolean, 是否允许返回空字符串
	 *                    true  -- 若传入为空或非法则返回 '' 
	 *                    false -- 若传入为空或非法则返回 StringUtil.defaultTime
	 * @return String, 转换后的字符串
	 */
	public static String formatDate(Object o, boolean ifAllowNull) {
	    String baseReturnTime = "";
	    if(!ifAllowNull) {
	        baseReturnTime = StringUtil.defaultTime;
	    }
		if (o == null || o.equals("")) {
			return "'" + baseReturnTime + "'";
		}

		String str = o.toString().trim();
		SimpleDateFormat sdf = null;
		Date date = null;
		String time = null;
		
		if (str.length() == 4) {
			time = str + "-01-01 00:00:00";
		} else if (str.length() == 10) {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = sdf.parse(str);
				time = sdf.format(date) + " 00:00:00";
			} catch (ParseException e1) {
				return "'" + baseReturnTime + "'";
			}
		} else if (str.length() == 19) {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				date = sdf.parse(str);
				time = sdf.format(date);
			} catch (ParseException e1) {
				return "'" + baseReturnTime + "'";
			}
		} else {
			sdf = new SimpleDateFormat("yyyy");
			try {
				if (str.length() >= 4) {
					date = sdf.parse(str.substring(0, 4));
					time = sdf.format(date) + "-01-01 00:00:00";
				} else {
					return "'" + baseReturnTime + "'";
				}
			} catch (ParseException e2) {
				return "'" + baseReturnTime + "'";
			}
		}
		return "'" + time + "'";
	}
	
	public static void main(String[] args){
	    String s = formatDate("2006-01-01", true);
	    System.out.println("2006-01-01".length());
	    System.out.println(s.substring(1, 11));
	}
}